﻿using ERP.ERPModels.HumanResources.HumanResourceManagement;
using ERP.HumanResources.HumanResourceManagement.Dtos;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.HumanResources.HumanResourceManagement
{
    public class JobApplicationService:ApplicationService,IJobApplicationService
    {
        /// <summary>
        /// 入职申请仓储
        /// </summary>
        private readonly IRepository<JobApplication, int> _jobApplicationRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="jobApplicationRepository">入职申请仓储</param>
        public JobApplicationService(IRepository<JobApplication, int> jobApplicationRepository)
        {
            _jobApplicationRepository = jobApplicationRepository;
        }

        /// <summary>
        /// 入职申请回显
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<JobApplication> JobApplicationByIdList(int id)
        {
            var list = await _jobApplicationRepository.FindAsync(id);

            if (list == null)
            {
                return null;
            }

            return list;
        }
        
        /// <summary>
        /// 导出入职申请列表到Excel
        /// </summary>
        /// <param name="input">查询参数</param>
        /// <returns>Excel文件字节数组</returns>
        public async Task<byte[]> ExportToExcelAsync(JobApplicationPageListDTO input)
        {
            // 1. 构建查询
            var query = await _jobApplicationRepository.GetQueryableAsync();

            // 2. 应用筛选条件
            if (!string.IsNullOrWhiteSpace(input.PersonName))
            {
                query = query.Where(x => x.PersonName.Contains(input.PersonName));
            }

            if (!string.IsNullOrWhiteSpace(input.InterviewPeople))
            {
                query = query.Where(x => x.InterviewPeople.Contains(input.InterviewPeople));
            }

            // 3. 获取数据
            var data =  query
                .OrderByDescending(x => x.InterviewTime)
                .ToList();

            // 4. 创建Excel工作簿
            using var ms = new MemoryStream();
            IWorkbook workbook = new XSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("入职申请列表");

            // 5. 创建表头
            var headerRow = sheet.CreateRow(0);
            headerRow.CreateCell(0).SetCellValue("人员姓名");
            headerRow.CreateCell(1).SetCellValue("联系电话");
            headerRow.CreateCell(2).SetCellValue("性别");
            headerRow.CreateCell(3).SetCellValue("定级");
            headerRow.CreateCell(4).SetCellValue("面试反馈");
            headerRow.CreateCell(5).SetCellValue("面试结果");
            headerRow.CreateCell(6).SetCellValue("面试次数");
            headerRow.CreateCell(7).SetCellValue("面试时间");
            headerRow.CreateCell(8).SetCellValue("面试官");

            // 6. 设置表头样式
            var headerStyle = workbook.CreateCellStyle();
            var headerFont = workbook.CreateFont();
            headerFont.IsBold = true;
            headerStyle.SetFont(headerFont);
            
            for (int i = 0; i < 9; i++)
            {
                headerRow.GetCell(i).CellStyle = headerStyle;
            }

            // 7. 填充数据
            int rowIndex = 1;
            foreach (var item in data)
            {
                var row = sheet.CreateRow(rowIndex++);
                row.CreateCell(0).SetCellValue(item.PersonName);
                row.CreateCell(1).SetCellValue(item.Phone);
                row.CreateCell(2).SetCellValue(item.Sex == 0 ? "男" : "女");
                row.CreateCell(3).SetCellValue(item.GradingMatch);
                row.CreateCell(4).SetCellValue(item.InterviewFeedback);
                row.CreateCell(5).SetCellValue(item.InterviewResult);
                row.CreateCell(6).SetCellValue(item.InterviewNumber);
                row.CreateCell(7).SetCellValue(item.InterviewTime.ToString("yyyy-MM-dd HH:mm:ss"));
                row.CreateCell(8).SetCellValue(item.InterviewPeople);
            }

            // 8. 自动调整列宽
            for (int i = 0; i < 9; i++)
            {
                sheet.AutoSizeColumn(i);
            }

            // 9. 写入内存流并返回
            workbook.Write(ms);
            return ms.ToArray();
        }
    }
}
